package org.bulu.project.system.service.impl;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.bulu.project.system.service.LikeService;
import org.bulu.project.system.mapper.LikeMapper;



import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class LikeServiceImpl implements LikeService {
    private final RedisTemplate<String,Object> redisTemplate;
    private final LikeMapper likeMapper;

    @Override
    public Long insertLike(Integer uid, Integer vid) {
        String key = "like:" + vid;
         Long result = redisTemplate.opsForSet().add(key,uid);
        if (result>0) {
            // 同步到 MySQL
            likeMapper.insertLike(uid, vid);
        }
        return result;
    }

    @Override
    public Long deleteLike(Integer uid, Integer vid) {
        String key = "like:" + vid;
        Long result = redisTemplate.opsForSet().remove(key, uid);
        if (result>0) {
            // 删除 MySQL 中的记录
            likeMapper.deleteLike(uid, vid);
        }
        return result;
    }

    @Override
    public Long countLikes(Integer vid) {
        String key = "like:" + vid;
        return redisTemplate.opsForSet().size(key);
    }
}
